package com.matt.foundation.utils;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Ron
 * from com.matt
 * on 2018/7/18 0018.
 */
public class QueryUtil {

    /**
     *
     * @param resource
     * @return
     */
    public static String joinQuota(List<String> resource){
        List<String> b =new ArrayList<String>();
        for (String string : resource) {
            b.add("'"+string+"'");
        }
        String list = Pattern.compile("\\b([\\w\\W])\\b").matcher(b.toString().substring(1,b.toString().length()-1)).replaceAll("'$1'");
        return list;
    }

    /**
     *
     * @param str
     * @return
     */
    public static String strtosqlin(String str){
        List<String> resource =new ArrayList<String>();
        if (str.contains(",")){
            String[] arr=str.split(",");
            for (String s:arr){
                resource.add(s);
            }
        }else{
            resource.add(str);
        }
        return inSqlStr(resource);
    }

    /**
     *
     * @param resource
     * @return
     */
    public static String inSqlStr(List<String> resource){
        List<String> b =new ArrayList<String>();
        for (String string : resource) {
            b.add(""+string+"");
        }
        String list = Pattern.compile("\\b([\\w\\W])\\b").matcher(b.toString().substring(1,b.toString().length()-1)).replaceAll("'$1'");
        return list;
    }

    /**
     * 过滤掉sql中存在的"'"符号的问题
     * @param str
     * @return
     */
    public static String replaceAllSql(String str){
        String sqlStr="";
        if (StringUtils.isNotBlank(str))
           sqlStr=str.replaceAll("\'","");
        return sqlStr;
    }

    public static void main(String[] args) {
        List<String> b =new ArrayList<String>();
        b.add("1");
        b.add("2");
        b.add("3");
        b.add("4");
        System.out.println(joinQuota(b));
        System.out.println(strtosqlin("2,4,8"));
    }

}
